<?

class insurance {


	var $url = "http://test.blueinsurance.ie/XML_API/BLUEXMLTEST/BLUEXMLAPI.asp";
	var $age_type = array('people_infants','people_children','people_adults','people_senior','people_senior_plus');

// FUNCTION // Validate form ======================================
function validate_step_1($form_data){
	
	$my_array = array();
	
	if(!$form_data['area']) array_push($my_array, 'Please select your destination.');
	
	// DEPARTURE DATE
	$dd = substr($form_data['date_departure'], 0, 2); 
	$dm = substr($form_data['date_departure'], 3, 2); 
	$dy = substr($form_data['date_departure'], 6, 4); 
	if(!checkdate($dm,$dd,$dy)) array_push($my_array, 'The departure date you entered is incorrect.'); 
	
	// RETURN DATE
	$dd = substr($form_data['date_return'], 0, 2); 
	$dm = substr($form_data['date_return'], 3, 2); 
	$dy = substr($form_data['date_return'], 6, 4); 
	if(!checkdate($dm,$dd,$dy)) array_push($my_array, 'The return date you entered is incorrect.'); 
	
	if($form_data['date_departure'] < date('d/m/Y')) array_push($my_array, 'The departure date must be later then todays date.');
	if($form_data['date_departure'] > $form_data['date_return']) array_push($my_array, 'The departure date cannot be later then the return date.');
	
	// BOOKED DATE
	$dd = substr($form_data['date_booked'], 0, 2); 
	$dm = substr($form_data['date_booked'], 3, 2); 
	$dy = substr($form_data['date_booked'], 6, 4); 
	if(!checkdate($dm,$dd,$dy)) array_push($my_array, 'The holiday booked date you entered is incorrect.'); 
	
	// DISCLAIMER 3
	if($form_data['disclaimer_3']) array_push($my_array, 'You must be a resident for at least 6 months to purchase insurance.'); 
	
	// T & C
	if(!$form_data['terms']) array_push($my_array, 'Please confirm that you have read and agree with our policy terms and conditions.');
	
	// AGES
	if(($form_data['level'] == 'Premier') AND $form_data['people_senior_plus']) array_push($my_array, 'For seniors plus cover please select our Premier Plus policy.');
	
	return $my_array;

}
// END FUNCTION ==================================================








// FUNCTION // Build XML request ======================================
function build_xml_request($values){




	//print_r($_POST);
	header ("content-type: text/xml");
	
	// create a new XML document
	$doc = domxml_new_doc('1.0');
	
	// <TravelInsurance>
	$root = $doc->create_element('TravelInsurance');
	$root = $doc->append_child($root);
	
	// <ControlGroup>
	$node_cg = $doc->create_element('ControlGroup');
	$node_cg = $root->append_child($node_cg);
	
	// <AgencyID>947</AgencyID>
	$child = $doc->create_element('AgencyID');
	$child = $node_cg->append_child($child);
	$value = $doc->create_text_node('1109');
	$value = $child->append_child($value);
	
	// <UserID>3934</UserID>
	$child = $doc->create_element('UserID');
	$child = $node_cg->append_child($child);
	$value = $doc->create_text_node('5228');
	$value = $child->append_child($value);
	
	// <ITSupplier>0001</ITSupplier>
	$child = $doc->create_element('ITSupplier');                                                          
	$child = $node_cg->append_child($child);
	$value = $doc->create_text_node('0001');
	$value = $child->append_child($value);
	
	// <Password>XMLPass</Password>
	$child = $doc->create_element('Password');
	$child = $node_cg->append_child($child);
	$value = $doc->create_text_node('La8dEa!s');
	$value = $child->append_child($value);
			  
	// <MessageType>BIQS</MessageType>
	$child = $doc->create_element('MessageType');
	$child = $node_cg->append_child($child);
	$value = $doc->create_text_node('BIQS');
	$value = $child->append_child($value);
	
	// <PolicyDetails>
	$node_pd = $doc->create_element('PolicyDetails');
	$node_pd = $root->append_child($node_pd);
	
	// <CoverType>S</CoverType>
	$child = $doc->create_element('CoverType');
	$child = $node_pd->append_child($child);
	$value = $doc->create_text_node($values['type']);
	$value = $child->append_child($value);
	
	// <CoverLevel>Premier</CoverLevel>
	$child = $doc->create_element('CoverLevel');
	$child = $node_pd->append_child($child);
	$value = $doc->create_text_node($values['level']);
	$value = $child->append_child($value);
	
	// <Area>2</Area>
	$child = $doc->create_element('Area');
	$child = $node_pd->append_child($child);
	$value = $doc->create_text_node($values['area']);
	$value = $child->append_child($value);
	
	// <DepartDate>01/08/2008</DepartDate>
	$child = $doc->create_element('DepartDate');
	$child = $node_pd->append_child($child);
	$value = $doc->create_text_node($values['date_departure']);
	$value = $child->append_child($value);
	
	// <ReturnDate>01/09/2008</ReturnDate>
	$child = $doc->create_element('ReturnDate');
	$child = $node_pd->append_child($child);
	$value = $doc->create_text_node($values['date_return']);
	$value = $child->append_child($value);
	
	// <TwelveMonth>N</TwelveMonth>
	$child = $doc->create_element('TwelveMonth');
	$child = $node_pd->append_child($child);
	$value = $doc->create_text_node('N');
	$value = $child->append_child($value);
	
	// <Cover>Individual</Cover>
	$child = $doc->create_element('Cover');
	$child = $node_pd->append_child($child);
	$value = $doc->create_text_node('Individual');
	$value = $child->append_child($value);
	
	// <LateBook>N</LateBook>
	$child = $doc->create_element('LateBook');
	$child = $node_pd->append_child($child);
	$value = $doc->create_text_node('N');
	$value = $child->append_child($value);
	
	
	// <InsuredPersons>
	$node_ips = $doc->create_element('InsuredPersons');
	$node_ips = $root->append_child($node_ips);
	
	foreach($this->age_type as $type_name){
	
	for($i = 0; $i < $values[$type_name]; $i++){
		
		// <InsuredPerson>
		$node_ip = $doc->create_element('InsuredPerson');
		$node_ip = $node_ips->append_child($node_ip); // $node_ips
			
			// <FirstName>x</FirstName>
			$child = $doc->create_element('FirstName');
			$child = $node_ip->append_child($child);
			$value = $doc->create_text_node('x');
			$value = $child->append_child($value);
			
			// <SurName>x</SurName>
			$child = $doc->create_element('SurName');
			$child = $node_ip->append_child($child);
			$value = $doc->create_text_node('x');
			$value = $child->append_child($value);
			
			// <Age>20</Age>
			$child = $doc->create_element('Age');
			$child = $node_ip->append_child($child);
			$value = $doc->create_text_node($this->get_sample_age($type_name));
			$value = $child->append_child($value);
			
			// <AdditionalCovers>
			$node_acs = $doc->create_element('AdditionalCovers');
			$node_acs = $node_ip->append_child($node_acs); // $node_ip
			
				// <ExcessWaiver>N</ExcessWaiver>
				$child = $doc->create_element('ExcessWaiver');
				$child = $node_acs->append_child($child);
				$value = $doc->create_text_node($values['extras']['add_cover_xs']);
				$value = $child->append_child($value);

				// <Golf>N</Golf>
				$child = $doc->create_element('Golf');
				$child = $node_acs->append_child($child);
				$value = $doc->create_text_node($values['extras']['add_cover_golf']);
				$value = $child->append_child($value);
				
				// <Business>N</Business>
				$child = $doc->create_element('Business');
				$child = $node_acs->append_child($child);
				$value = $doc->create_text_node($values['extras']['add_cover_biz']);
				$value = $child->append_child($value);
				
				// <Wedding>N</Wedding>
				$child = $doc->create_element('Wedding');
				$child = $node_acs->append_child($child);
				$value = $doc->create_text_node($values['extras']['add_cover_wed']);
				$value = $child->append_child($value);
								
				// <WinterSports>N</WinterSports>
				$child = $doc->create_element('WinterSports');
				$child = $node_acs->append_child($child);
				$value = $doc->create_text_node($values['extras']['add_cover_win']);
				$value = $child->append_child($value);
				
				// <BaggageDeletion>N</BaggageDeletion>
				$child = $doc->create_element('BaggageDeletion');
				$child = $node_acs->append_child($child);
				$value = $doc->create_text_node('N');
				$value = $child->append_child($value);
				
				// <IncreasedCancellation>N</IncreasedCancellation>
				$child = $doc->create_element('IncreasedCancellation');
				$child = $node_acs->append_child($child);
				$value = $doc->create_text_node('N');
				$value = $child->append_child($value);
				
			
			// <HazardousActivities>
			$node_ha = $doc->create_element('HazardousActivities');
			$node_ha = $node_ip->append_child($node_ha); // $node_ip
				
				
				// <HAGrade>Grade 1</HAGrade>
				$child = $doc->create_element('HAGrade');
				$child = $node_ha->append_child($child);
				$value = $doc->create_text_node('Grade 1');
				$value = $child->append_child($value);
			
			
			
			// <Discounts>
			$node_ds = $doc->create_element('Discounts');
			$node_ds = $node_ip->append_child($node_ds); // $node_ip
				
				// <PMIDiscount>
				$node_pmi = $doc->create_element('PMIDiscount');
				$node_pmi = $node_ds->append_child($node_pmi); // $node_ds
				
					
					// <PMIType/>Bupa<PMIType/>
					$child = $doc->create_element('PMIType');
					$child = $node_pmi->append_child($child);
					//$value = $doc->create_text_node('Bupa');
					//$value = $child->append_child($value);
					
					
					// <PMINumber/>12345346890p<PMINumber/>
					$child = $doc->create_element('PMINumber');
					$child = $node_pmi->append_child($child);
					//$value = $doc->create_text_node('12345346890p');
					//$value = $child->append_child($value);
			
	
	} // END PERSONS LOOP
	
	}
	
	// get completed xml document
	$xml_string = $doc->dump_mem(true);
	
	
	
	return $xml_string;

	
}
// END FUNCTION ==================================================



// FUNCTION // Send XML request ======================================
function send_xml_request($xml_data){





	/*$request_file = "form_1_process.php";
	$fh = fopen($request_file, 'r');
	$xml_data = fread($fh, filesize($xml_doc));
	fclose($fh); 
	$url = "http://test.blueinsurance.ie/XML_API/BLUEXMLTEST/BLUEXMLAPI.asp";*/
	
	$page = "/services/calculation"; // ???
	
	$headers = array( "POST ".$page." HTTP/1.0",
	"Content-type: text/xml;charset=\"utf-8\"",
	"Accept: text/xml",
	"Cache-Control: no-cache",
	"Pragma: no-cache",
	"SOAPAction: \"run\"",
	"Content-length: ".strlen($xml_data),
	"Authorization: Basic ".base64_encode($credentials));
	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $this->url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 60);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);
	
	// Apply the XML to our curl call
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
	
	$data = curl_exec($ch);
	
	if(curl_errno($ch)) {
		return print "Error: ".curl_error($ch);
	}else{
		// Show me the result
		return $data;
		curl_close($ch);
	}


}
// END FUNCTION ==================================================




// FUNCTION // Get destination from value ========================
function get_destination($value){


	switch ($value){
		case "1";
			return 'Ireland/UK';
		break;
		case "2";
			return 'Europe';
		break;
		case "3";
			return 'Aus/NZ';
		break;
		case "4";
			return 'Worldwide';
		break;
		case "5";
			return 'Worldwide Inc USA/Canada';
		break;
	}
	

}
// END FUNCTION ==================================================



// FUNCTION // Convert age level to sample age ========================
function get_sample_age($age_level){


	switch ($age_level){
		case "people_infants";
			// 0 - 2 years
			return '1';
		break;
		case "people_children";
			// 3 - 18 years
			return '13';
		break;
		case "people_adults";
			// 19 - 67 years
			return '33';
		break;
		case "people_senior";
			// 68 - 79 years
			return '73';
		break;
		case "people_senior_plus";
			// 80 - 85 years
			return '83';
		break;
	}
	

}
// END FUNCTION ==================================================






}
?>